//
//  RHInverterParaContentCell.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2024/6/13.
//

#import "RHInverterParaContentCell.h"
@interface RHInverterParaContentCell()

@property (nonatomic, strong) NSMutableArray *itemsArray;


@end

@implementation RHInverterParaContentCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return self;
}
-(void)setModelArray:(NSArray<DeviceChartFieldModel *> *)modelArray
{
    _modelArray = modelArray;
    [modelArray enumerateObjectsUsingBlock:^(DeviceChartFieldModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if(idx < self.itemsArray.count){
            //对每个lab赋值
            QMUILabel *lab = self.itemsArray[idx];
            lab.font = kSYSTEMFONT(13);
            lab.textColor = kCOLOR_HEX_A(@"#3F4443", 0.6);
            NSDecimalNumber *value = [NSDecimalNumber decimalNumberWithString:ValidNullDataToString(obj.content, @"0")];
            NSString *valueStr = [NSString stringWithFormat:@"%@",value];
            lab.text = ValidNullDataToString(valueStr, @"--");
        }
    }];
    //针对不同的个数重新布局
    [self updateLabel];
}

//重新布局
-(void)updateLabel
{
    kWeakSelf;
    [self.itemsArray enumerateObjectsUsingBlock:^(QMUILabel*  _Nonnull lab, NSUInteger idx, BOOL * _Nonnull stop) {
        [lab removeFromSuperview];
    }];
    NSInteger count = self.modelArray.count==1?3:self.modelArray.count;
    //超过3个按3个算
    if (self.modelArray.count) {
        count = 3;
    }
    //左边第一个标题lebel的宽度
    CGFloat titleWidth = 70;
    CGFloat itemtMargin = 5;
    CGFloat labelWidth = (kUI_WINDOW_W - 30 - titleWidth - ((count+1)*itemtMargin))/count;
    
    [self.titleLab mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.offset(titleWidth);
    }];
    
    self.titleLab.textAlignment = NSTextAlignmentLeft;
    //底部一个数据的,可以将标题调宽一点
    if(self.modelArray.count == 1){
        
        [self.titleLab mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.offset(200);
        }];
        
        //第一个标题label
        QMUILabel *firstLab = [self.itemsArray firstObject];
        [_bgView addSubview:firstLab];
        [firstLab mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(weakSelf.bgView.mas_left).offset(kUI_WINDOW_W-30);
            make.centerY.mas_offset(0);
            make.width.offset(labelWidth);
        }];
        DeviceChartFieldModel *fieldModel = [self.modelArray firstObject];
        //使用后台翻译
        self.titleLab.text = fieldModel.keyName;
        self.titleLab.font = kSYSTEMFONT(14);
        NSString *numStr = [NSDecimalNumber decimalPlacesWithoutRoundDownWithDigit:3 value:[fieldModel.content doubleValue]];
        NSString *value = [NSString stringWithFormat:@"%@ (%@)",ValidNullDataToString(numStr, @"--"),fieldModel.unitName];
        NSString *key = [NSString stringWithFormat:@"(%@)",fieldModel.unitName];
        firstLab.font = kSYSTEMFONT(16);
        firstLab.textColor = kCOLOR_HEX(@"#3F4443");
        firstLab.attributedText = [self attrStringWithContent:value attrKey:key];
        //储充一机放有版本信息,直接显示
        if (fieldModel.unitName.length == 0) {
            firstLab.text = ValidNullDataToString(fieldModel.content, @"--");
        }
        
        //枚举类型
        if (fieldModel.isEnum) {
            
            //找出对应下标的枚举值
            __block NSInteger index = -1;
            [fieldModel.enumIndexs enumerateObjectsUsingBlock:^(NSString*  _Nonnull enumIndex, NSUInteger idx, BOOL * _Nonnull stop) {
                if ([fieldModel.content integerValue] == enumIndex.integerValue) {
                    index = idx;
                }
            }];
            //找到了就显示对应的枚举值
            if (index != -1) {
                firstLab.text = [NSString stringWithFormat:@"%@%@",fieldModel.enumValues[index],fieldModel.unitName];
            }else{
                //没找到,显示后台返回的值
                firstLab.text = ValidNullData(fieldModel.content);
            }
            
        }
    }else{
        self.titleLab.font = kSYSTEMFONT(13);
        
        [self.modelArray enumerateObjectsUsingBlock:^(DeviceChartFieldModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if(idx<self.itemsArray.count){
                QMUILabel *lab = self.itemsArray[idx];
                NSLog(@"----->赋值:key = %@ name=%@ value=%@",obj.keyCode,obj.keyName,obj.content);
               
                [_bgView addSubview:lab];
                [lab mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_offset(itemtMargin+titleWidth+ itemtMargin +(labelWidth+itemtMargin)*idx);
                    make.width.offset(labelWidth);
                    make.centerY.mas_offset(0);
                }];
            }
        }];
    }
}

-(NSAttributedString*)attrStringWithContent:(NSString *)content  attrKey:(NSString*)key
{
    NSDictionary *attiDic = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:10],NSForegroundColorAttributeName:[UIColor colorWithHexString:@"3F4443" alpha:0.6]};
    
    NSMutableAttributedString *attri_content = [[NSMutableAttributedString alloc] initWithString:content];
    [attri_content setAttributes:attiDic range:[content rangeOfString:key]];
    return attri_content;
}

//-(void)setTitleArr:(NSArray *)titleArr
//{
//    _titleArr = titleArr;
//    [titleArr enumerateObjectsUsingBlock:^(NSString*  _Nonnull title, NSUInteger idx, BOOL * _Nonnull stop) {
//        if (idx < self.itemsArray.count) {
//            QMUILabel *titleLab = self.itemsArray[idx];
//            titleLab.text = title.rh_localized;
//        }
//    }];
//}

- (void)setupUI{
    self.contentView.backgroundColor = CFTabViewBgColor;
    UIView *bgView = kView;
    bgView.backgroundColor = UIColor.whiteColor;
    [self.contentView addSubview:bgView];
    self.bgView = bgView;
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0, 0, 0, 0));
        make.height.offset(35);
    }];
    

    self.itemsArray = [NSMutableArray array];
    CGFloat bgWidth = MIN(kUI_WINDOW_W, kUI_WINDOW_H)-30;
    __block QMUILabel *lastview;
    //暂且创建6个label,有需要再加
    NSArray *titles = @[@"--",@"--",@"--",@"--",@"--",@"--",@"--",@"--",@"--"];
    CGFloat itemWidth = bgWidth / (titles.count+1);
   
    [titles enumerateObjectsUsingBlock:^(NSString*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        QMUILabel *titleLab  = [QMUILabel labelWithFont:kSYSTEMFONT(13) textColor:kCOLOR_HEX_A(@"#3F4443", 0.6) textAligment:NSTextAlignmentCenter];
        titleLab.text = titles[idx];
        titleLab.numberOfLines = 2;
        titleLab.adjustsFontSizeToFitWidth = YES;
        titleLab.minimumScaleFactor = 0.5;
        [bgView addSubview:titleLab];
//        titleLab.backgroundColor = [UIColor randomColor];
        if (lastview) {
            [self.itemsArray addObject:titleLab];
            [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(lastview.mas_right);
                make.centerY.mas_offset(0);
                make.width.offset(itemWidth);
            }];
        }else{
            self.titleLab = titleLab;
            [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_offset(5);
                make.centerY.mas_offset(0);
                make.width.offset(itemWidth-5);
            }];
        }
        lastview = titleLab;
    }];
    
}


@end
